/*
 * Copyright (C) 2017 Samuel Wall
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package uk.co.samuelwall.materialtaptargetprompt.extras.backgrounds;

import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.Point;
import ohos.agp.utils.Rect;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;
import uk.co.samuelwall.materialtaptargetprompt.extras.PromptBackground;
import uk.co.samuelwall.materialtaptargetprompt.extras.PromptOptions;
import uk.co.samuelwall.materialtaptargetprompt.extras.PromptUtils;
import uk.co.samuelwall.materialtaptargetprompt.utils.Constants;
import uk.co.samuelwall.materialtaptargetprompt.utils.DensityUtils;

/**
 * {@link PromptBackground} implementation that renders the prompt background as a rectangle.
 *
 * @since 2021-06-17
 */
public class RectanglePromptBackground extends PromptBackground {
    RectFloat mBounds;
    RectFloat mBaseBounds;
    Paint mPaint;
    int mBaseColourAlpha;
    float mRx;
    float mRy;
    Point mFocalCentre;

    /**
     * Constructor.
     *
     * @param context context
     */
    public RectanglePromptBackground(Context context) {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mBounds = new RectFloat();
        mBaseBounds = new RectFloat();
        mFocalCentre = new Point();
        final float density = DensityUtils.getDeviceAttr(context).getAttributes().densityPixels;
        mRx = Constants.NUM_2 * density;
        mRy = Constants.NUM_2 * density;
    }

    /**
     * Set the radius for the rectangle corners.
     *
     * @param rx The x-radius of the oval used to round the corners
     * @param ry The y-radius of the oval used to round the corners
     * @return This prompt background
     */
    public RectanglePromptBackground setCornerRadius(final float rx, final float ry) {
        mRx = rx;
        mRy = ry;
        return this;
    }

    /**
     * setColour
     *
     * @param colour Colour integer representing the colour.
     */
    @Override
    public void setColour(int colour) {
        mPaint.setColor(new Color(colour));
        mBaseColourAlpha = Color.alpha(colour);
        mPaint.setAlpha(mBaseColourAlpha);
    }

    @Override
    public void prepare(final PromptOptions options, final boolean isClipToBounds, Rect clipBounds) {
        mPromptOptions = options;
        final RectFloat focalBounds = options.getPromptFocal().getBounds();
        final RectFloat textBounds = options.getPromptText().getBounds();
        final float textPadding = options.getTextPadding();
        float x1;
        float x2;
        float y1;
        float y2;
        if (textBounds.top < focalBounds.top) {
            y1 = textBounds.top - textPadding;
            y2 = focalBounds.bottom + textPadding;
        } else {
            y1 = focalBounds.top - textPadding;
            y2 = textBounds.bottom + textPadding;
        }
        x1 = Math.min(textBounds.left - textPadding,
                focalBounds.left - textPadding);
        x2 = Math.max(textBounds.right + textPadding,
                focalBounds.right + textPadding);
        mBaseBounds.modify(x1, y1, x2, y2);
        mFocalCentre.modify(focalBounds.getCenter().getPointX(), focalBounds.getCenter().getPointY());
    }

    @Override
    public void update(float revealModifier, float alphaModifier) {
        mPaint.setAlpha(mBaseColourAlpha * alphaModifier / Constants.NUM_255);
        PromptUtils.scale(mFocalCentre, mBaseBounds, mBounds, revealModifier, false);
    }

    @Override
    public void draw(Canvas canvas) {
        canvas.drawRoundRect(mBounds, mRx, mRy, mPaint);
    }

    /**
     * contains
     *
     * @param x x coordinate.
     * @param y y coordinate.
     * @return boolean
     */
    @Override
    public boolean contains(float x, float y) {
        return mBounds.isInclude(x, y);
    }
}
